package com.lagou.client;

import com.lagou.req.RequestInfo;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author xsl
 * @date 2021/4/4 21:52
 * @Description: RpcConsumerManager
 */
public class RpcConsumerManager {

    private static Map<String, RpcConsumer> rpcConsumerMap = new HashMap<>();

    private static Map<String, RequestInfo> requestInfoMap = new ConcurrentHashMap<>();

    public static void addRpcConsumer(String server) {
        if (StringUtils.isEmpty(server) || server.indexOf(":") == 0) {
            return;
        }
        try{
            String[] strs = server.split(":");
            RpcConsumer rpcConsumer = new RpcConsumer(strs[0], Integer.valueOf(strs[1]));
            rpcConsumerMap.put(server, rpcConsumer);
            System.out.println("服务端：" + server + "上线建立连接");
        }catch (Exception e){
//            e.printStackTrace();
        }
    }

    public static void removeRpcConsumer(String server) {
        RpcConsumer rpcConsumer = rpcConsumerMap.get(server);
        if (rpcConsumer != null) {
            rpcConsumer.closeConnect();
            rpcConsumerMap.remove(server);
            System.out.println("服务端：" + server + "下线断开连接");
        }
    }

    public static void rpcConsumerMapChanged(List<String> servers) {
        if (servers == null || servers.size() == 0) {
            for (String key : rpcConsumerMap.keySet()) {
                removeRpcConsumer(key);
            }
        } else {
            for (String key : rpcConsumerMap.keySet()) {
                if (servers.contains(key)) {
                    continue;
                } else {
                    removeRpcConsumer(key);
                }
            }
            for (String server : servers) {
                if (rpcConsumerMap.containsKey(server)) {
                    continue;
                } else {
                    addRpcConsumer(server);
                }
            }
        }
    }

    public static Map<String, RpcConsumer> getRpcConsumerMap() {
        return rpcConsumerMap;
    }

    public static Map<String, RequestInfo> getRequestInfoMap() {
        return requestInfoMap;
    }

    public static void addRequestInfo(String key, RequestInfo requestInfo){
        requestInfo.setRpcConsumer(rpcConsumerMap.get(key));
        requestInfoMap.put(key, requestInfo);
    }

    public static void removeRequestInfo(String key){
        if(requestInfoMap.containsKey(key)){
            requestInfoMap.remove(key);
        }
    }


    public static void removeServer(String key){
        removeRpcConsumer(key);
        removeRequestInfo(key);
    }


    public static void removeAllServer(){
        rpcConsumerMapChanged(null);
        removeAllRequestInfo();
    }

    public static void removeAllRequestInfo(){
        for (String key : requestInfoMap.keySet()) {
            requestInfoMap.remove(key);
        }
    }
}
